//
//  QRCodeView.m
//  TestDemo
//
//  Created by myApple on 16/2/24.
//  Copyright © 2016年 myApple. All rights reserved.
//

#import "QRCodeView.h"
#import "Constants.h"
#import "HexStringToGeneralString.h"
#import "ZXingObjC.h"

@interface QRCodeView()
{
    UIImageView * QRCodeImageView;//二维码
    UIImageView * logoImageView;//logo
    UILabel * cardNameLable;    //二维码房卡的名称
    UILabel * copyRightLable;   //版权
    UIImageView * ADImageView;  //广告图片
}

@end

@implementation QRCodeView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [[UIApplication sharedApplication] setApplicationSupportsShakeToEdit:YES];
        [self canBecomeFirstResponder];
        float x = UI_SCREEN_WIDTH / 2.0 - 100;
        float y = 0;
        float width = 200;
        float height = 200;
        //二维码
        QRCodeImageView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
        QRCodeImageView.userInteractionEnabled = YES;
        [self addSubview:QRCodeImageView];
        UITapGestureRecognizer * refreshTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(refreshQRCodeClick)];
        [QRCodeImageView addGestureRecognizer:refreshTap];
        
        //logo图片
        x = QRCodeImageView.frame.size.width / 2.0 - 15;
        y = x;
        width = 30;
        height = 30;
        logoImageView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
        [QRCodeImageView addSubview:logoImageView];
        
        //二维码房卡的名称
        x = 0;
        y = QRCodeImageView.frame.size.height + QRCodeImageView.frame.origin.y;
        width = UI_SCREEN_WIDTH;
        height = 20;
        cardNameLable = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
        cardNameLable.textColor = [UIColor blackColor];
        cardNameLable.textAlignment = NSTextAlignmentCenter;
        cardNameLable.font = [UIFont systemFontOfSize:14.0];
        [self addSubview:cardNameLable];
        
        
        //点击刷新
        x = 0;
        y += height + 10;
        UILabel * refreshLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
        refreshLabel.text = @"点击二维码可以刷新";
        refreshLabel.textAlignment = NSTextAlignmentCenter;
        refreshLabel.textColor = UIColorFrom0x(0x957D52);
        refreshLabel.font = [UIFont systemFontOfSize:14.0];
        [self addSubview:refreshLabel];
        
        
        //广告图片
        x = 20;
        y += height + 10;
        width = UI_SCREEN_WIDTH - 2 * x;
        height = (int)(width / 3.0);
        ADImageView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
        [self addSubview:ADImageView];
        
        //版权
        x = 0;
        y = UI_SCREEN_HEIGHT - 30 - 64;
        width = UI_SCREEN_WIDTH;
        height = 20;
        copyRightLable = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
        copyRightLable.textAlignment = NSTextAlignmentCenter;
        copyRightLable.textColor = [UIColor blackColor];
        copyRightLable.font = [UIFont systemFontOfSize:13.0];
        [self addSubview:copyRightLable];
        
    }
    
    return self;
}

#pragma mark ---------- 各个view中赋值
-(void)setInfoDict:(NSDictionary *)infoDict
{
    [self canBecomeFirstResponder];
    NSDictionary * dataDict = [infoDict objectForKey:@"data"];
    NSString * QRString = [dataDict objectForKey:@"qrcodeString"];
    //字符串的转换
    HexStringToGeneralString * hex = [[HexStringToGeneralString alloc]init];
    QRString = [hex stringFromHexString:QRString];
    
    NSString * logoUrl = [dataDict objectForKey:@"logoUrl"];
    NSString * communtiyName = [dataDict objectForKey:@"communityName"];
    NSString * buildName = [dataDict objectForKey:@"buildName"];
    NSString * floorName = [dataDict objectForKey:@"floorName"];
    NSString * roomName = [dataDict objectForKey:@"roomName"];
    NSString * commercialUrl = [dataDict objectForKey:@"commercialUrl"];
    
    ZXImage * xImage = [hex prepareCardView:QRString];
    QRCodeImageView.image = [UIImage imageWithCGImage:xImage.cgimage];
    logoImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:logoUrl]]];
    cardNameLable.text = [NSString stringWithFormat:@"%@%@%@%@",communtiyName,buildName,floorName,roomName];
    ADImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:commercialUrl]]];
    copyRightLable.text = @"©2014-2016 UCLBRT.All rights reserved";
    

}


#pragma mark ---------- 点击刷新
-(void)refreshQRCodeClick
{
    if (self.refreshByRoom) {
        self.refreshByRoom();
    }
    
    if (self.refreshById) {
        self.refreshById();
    }
    
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
